package com.xyzboom.tld;

import com.xyzboom.web.servlets.resourse.ResourceHandler;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.jsp.JspException;
import jakarta.servlet.jsp.tagext.Tag;
import jakarta.servlet.jsp.tagext.TagSupport;

import java.io.IOException;

public class ResourcePathTag extends TagSupport {
    private String type;
    private String name;
    private String tail;

    public String getTail() {
        return tail;
    }

    public void setTail(String tail) {
        this.tail = tail;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int doStartTag() throws JspException {
        return Tag.SKIP_BODY;//不执行标签体内容
//            return Tag.EVAL_BODY_INCLUDE; //执行标签体内容
    }

    @Override
    public int doEndTag() throws JspException {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        try {
            String result = ResourceHandler.getResourcePath(type, name, request);
            if (tail != null) {
                result += tail;
            }
            pageContext.getOut().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Tag.EVAL_PAGE;//执行JSP页面
    }
}
